package org.hyl.javaext.sf;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class CMapDbl {
	private int maxlen = 5;
	private Map<BigDecimal, BigDecimal> maps = new ConcurrentHashMap<BigDecimal, BigDecimal>();
	
	
	 public static List<BigDecimal> sortByValue(Set<BigDecimal> set){  
	        List<BigDecimal> setList= new ArrayList<BigDecimal>(set);  
	        Collections.sort(setList, new Comparator<BigDecimal>() {  
	            @Override  
	            public int compare(BigDecimal o1, BigDecimal o2) {  
	                // TODO Auto-generated method stub  
	                return o2.compareTo(o1);  //����Ҫ��  
	            }  	              
	        });  
	       //����ע��ʹ��ListHashSet  
	        return setList;  
	}  
	public int getLen() {
		return maps.size();
	}
	//�ɽ��ͳ���
	public void remove(BigDecimal key, BigDecimal value) {
		if (maps.containsKey(key)) {
			BigDecimal v = maps.get(key);
			if (v.compareTo(value)>0) {				
				maps.put(key, v.subtract(value) );
			}else {
				maps.remove(key);			
		
			}
		}
	}
	//ί��
	public void add(BigDecimal key, BigDecimal value) {
		if (maps.containsKey(key)) {
			BigDecimal v = maps.get(key);
			maps.put(key, v.add(value));
		}else {
			maps.put(key,value);	
		}
	
	}
	//��Ŀ��ѯ��
	public void foreach(Itestfun itf){
		Map<BigDecimal, BigDecimal> t=new ConcurrentHashMap<BigDecimal, BigDecimal>();
		 List<BigDecimal> set1= sortByValue(maps.keySet());
		 int i=0;		
		 for (BigDecimal a :set1) {
			// System.out.prBigDecimalln("ccc"+a);			
			 if (i++>=maxlen) break;
			 if (itf!=null) itf.exec();
			 System.out.println(a+", "+maps.get(a));
			
		 }		
		 System.out.println("--------------------");
	}

}
